﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using C3.Directory.Repo;
using C3.Directory.Services;
using CSI.Directory.Core.Repositories;
using CSI.Directory.Core.Services;
using CSI.Directory.Repo;
using CSI.Directory.Services;
using StructureMap;
using StructureMap.Attributes;

namespace CSI.Directory.Web
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            
            routes.MapRoute(
                "Directory.SubCategories", "Directory/GetCategory/{id}/{page}",
                new { controller = "Directory", action = "GetCategory", id = 1, page = 1 });
            routes.MapRoute(
               "Directory.Listings", "Directory/Listing/{id}/{name}",
               new { controller = "Directory", action = "Listing", id = 1, name = "" });

            routes.MapRoute(
               "Directory.County", "Directory/County/{name}/{page}",
               new { controller = "Directory", action = "County", id = 1, name = "", page = 1 });

            
            routes.MapRoute(
               "Home.Article", "Home/Article/{id}/{name}",
               new { controller = "Home", action = "Article", id = 1, name="" });

            routes.MapRoute(
                "Empty", "",
                new { controller = "Home", action = "Index", id = "" });
            routes.MapRoute(
                "Index", "Index",
                new { controller = "Home", action = "Index", id = "" });
            routes.MapRoute(
                "Default", "Default.aspx",
                new { controller = "Home", action = "Index", id = "" });

            routes.MapRoute(
                "Controller.Action.Id",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
                );

            routes.MapRoute(
                "Controller.Action",
                "{controller}/{action}",
                new { action = "Index" }
                );


            routes.MapRoute(
                "Controller",
                "{controller}",
                new { action = "Index" }
                );
            
            //routes.MapRoute(
            //    "C3.Web.Controllers.Action.Id.FileName", "{controller}/{action}/{id}/{year}/{day}/{month}/{fileName}",
            //    new { id = 0, year = 0, day = 0, month = 0, fileName = string.Empty });

            //routes.MapRoute(
            //    "C3.Web.Controllers.Action.Id", "{controller}/{action}/{id}",
            //    null);
        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
            ObjectFactory.Initialize(service =>
                                         {


                                             service.ForRequestedType<IDirectoryRepository>().TheDefaultIsConcreteType<DirectoryRepository>().CacheBy(InstanceScope.Singleton);
                                             service.ForRequestedType<IManagerService>().TheDefaultIsConcreteType<ManagerService>().CacheBy(InstanceScope.HttpSession);
                                             service.ForRequestedType<IDirectoryService>().TheDefaultIsConcreteType<DirectoryService>().CacheBy(InstanceScope.HttpSession);
                                             service.ForRequestedType<IUserService>().TheDefaultIsConcreteType<UserService>().CacheBy(InstanceScope.HttpSession);
                                             service.ForRequestedType<ICacheManager>().TheDefaultIsConcreteType<CacheManager>().CacheBy(InstanceScope.Singleton);
                                             service.ForRequestedType<ISessionService>().TheDefaultIsConcreteType<SessionService>().CacheBy(InstanceScope.HttpSession);
                                             service.ForRequestedType<ITransactionService>().TheDefaultIsConcreteType<TransactionService>().CacheBy(InstanceScope.HttpSession);
                                             service.ForRequestedType<IProfileRepository>().TheDefaultIsConcreteType<ProfileRepository>().CacheBy(InstanceScope.Singleton);
                                             service.ForRequestedType<IUserRepository>().TheDefaultIsConcreteType<UserRepository>().CacheBy(InstanceScope.Singleton);

                                         });


        }
    }
}